<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/style/admin.css" media="all">
</head>

<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">加油记录</div>
                <div class="layui-card-body">
                    <div style="margin-bottom: 10px;">
                        <div class="layui-input-item" style="float:left;padding-right:10px;">
                            <form class="layui-form">
                                <div class="layui-inline">
                                    <input id="search-vehicle-name" type="text" placeholder="请输入车牌号"
                                           class="layui-input layui-input-search">
                                </div>
                                <div class="layui-inline">
                                    <input id="search-driver-name" type="text" placeholder="请输入驾驶员"
                                           class="layui-input layui-input-search">
                                </div>
                                <div class="layui-inline">
                                    <input id="search-start-date-range" type="text" placeholder="请输入加油日期范围"
                                           class="layui-input layui-input-search">
                                </div>
                                <div class="layui-inline">
                                    <select id="search-state">
                                        <option value="">请选择状态</option>
                                        <option value="1">待审核</option>
                                        <option value="2">已审核</option>
                                    </select>
                                </div>
                                <div class="layui-inline">
                                    <a id="btn-search" class="layui-btn">查找</a>
                                </div>
                            </form>
                        </div>
                        <div style="float:right;">
                            <a id="btn-refresh" class="layui-btn layui-btn-primary">刷新</a>
                            <a id="btn-batch-delete" class="layui-btn layui-btn-danger">删除</a>
                        </div>
                        <div style="clear:both;"></div>
                    </div>
                    <table class="layui-hide" id="vehicle-refuel-table" lay-filter="vehicle-refuel-table"></table>
                    <script type="text/html" id="toolbar-operation">
                        <a class="layui-btn layui-btn-xs" lay-event="edit">详情</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="vehicle-refuel-form">
    <div class="layui-card">
        <div class="layui-card-header">加油详情</div>
        <div class="layui-card-body">
            <form class="layui-form" action="#" lay-filter="vehicle-refuel-form">
                <input type="hidden" name="id">
                <div class="layui-form-item">
                    <label class="layui-form-label">车牌号</label>
                    <div class="layui-input-block">
                        <input type="text" name="vehicleName" class="layui-input" lay-verify="required" readonly>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">驾驶员</label>
                    <div class="layui-input-block">
                        <input type="text" name="driverName" class="layui-input" lay-verify="required" readonly>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">加油时间</label>
                    <div class="layui-input-block">
                        <input type="text" name="createTime" class="layui-input" readonly>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">结束里程</label>
                    <div class="layui-input-block">
                        <input type="text" name="mileage" class="layui-input" lay-verify="required" readonly>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">上次余额</label>
                    <div class="layui-input-block">
                        <input type="text" name="lastBalance" class="layui-input" lay-verify="required" readonly>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">加油金额</label>
                    <div class="layui-input-block">
                        <input type="text" name="expense" class="layui-input" lay-verify="required" readonly>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">本次余额</label>
                    <div class="layui-input-block">
                        <input type="text" name="thisBalance" class="layui-input" lay-verify="required" readonly>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">审核人</label>
                    <div class="layui-input-block">
                        <input type="text" name="auditByName" class="layui-input" readonly>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">审核时间</label>
                    <div class="layui-input-block">
                        <input type="text" name="auditTime" class="layui-input" readonly>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">审核结果</label>
                    <div class="layui-input-block">
                        <select name="auditResult">
                            <option value="">请选择审核结果</option>
                            <option value="1">通过</option>
                            <option value="2">退回</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">审核意见</label>
                    <div class="layui-input-block">
                        <textarea name="auditComment" class="layui-textarea"></textarea>
                    </div>
                </div>
                <!--<div class="layui-form-item">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">
                        <input type="text" name="stateText" class="layui-input" readonly>
                    </div>
                </div>-->
                <div class="layui-form-item">
                    <button id="audit-btn" class="layui-btn layui-btn-fluid" lay-submit lay-filter="form-audit">审核
                    </button>
                </div>
            </form>
        </div>
    </div>
</script>
<script src="/layui/layui.js"></script>
<script>
    layui.config({
        base: '/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'form', 'laydate'], function () {
        var $ = layui.$;
        var table = layui.table;
        var form = layui.form;
        var admin = layui.admin;
        var laydate = layui.laydate;
        var popupIndex;

        laydate.render({
            elem: '#search-start-date-range',
            range: true
        });

        table.render({
            elem: '#vehicle-refuel-table',
            url: 'vehicle-refuels',
            title: '',
            skin: 'line',
            cols: [[
                {type: 'checkbox'},
                {
                    title: '序号', width: 60, templet: function (d) {
                        return d.LAY_TABLE_INDEX + 1;
                    }
                },
                {field: 'vehicleName', title: '车牌号'},
                {field: 'driverName', title: '驾驶员'},
                {field: 'createTime', title: '加油时间'},
                {field: 'mileage', title: '加油里程(公里)'},
                {field: 'lastBalance', title: '上次余额'},
                {field: 'expense', title: '加油金额'},
                {field: 'thisBalance', title: '本次余额'},
                {field: 'auditResultText', title: '审核结果', width: 120},
                {field: 'auditComment', title: '审核意见', width: 120},
                {field: 'stateText', title: '状态'},
                {fixed: 'right', title: '操作', toolbar: '#toolbar-operation'}
            ]],
            page: true
        });

        function showVehicleRefuelForm() {
            popupIndex = admin.popupRight({
                area: '360px',
                success: function () {
                    var formHtml = $('#vehicle-refuel-form').html();
                    $('#' + this.id).html(formHtml);
                    form.render();
                    laydate.render({
                        elem: '#start-time'
                    });
                    laydate.render({
                        elem: '#end-time'
                    });
                    laydate.render({
                        elem: '#audit-time'
                    });
                }
            });
            return popupIndex;
        }

        function closePopupForm() {
            if (popupIndex) {
                layer.close(popupIndex);
            }
        }

        function search() {
            table.reload('vehicle-refuel-table', {
                where: {
                    vehicleName: $('#search-vehicle-name').val(),
                    startDateRange: $('#search-start-date-range').val(),
                    driverName: $('#search-driver-name').val(),
                    state: $('#search-state').val()
                },
                page: {
                    curr: 1
                }
            });
        }

        $(document).on('keypress', function (e) {
            if (e.which == 13) {
                search();
            }
        });

        $("#btn-search").on('click', function () {
            search();
        });

        $("#btn-refresh").on('click', function () {
            table.reload('vehicle-refuel-table');
            layer.msg('表格已刷新');
        });

        $("#btn-batch-delete").on('click', function () {
            layer.confirm('确定要删除吗?', {icon: 3, title: '提示'}, function (index) {
                var ids = [];
                var data = table.checkStatus('vehicle-refuel-table').data;
                for (let i in data) {
                    ids.push(data[i].id);
                }
                $.ajax({
                    url: 'vehicle-refuels/' + ids.toString(),
                    type: 'DELETE',
                    success: function (result) {
                        if (result.code == 200) {
                            layer.msg('删除成功');
                            table.reload('vehicle-refuel-table');
                        } else {
                            layer.msg('删除失败');
                        }
                    }
                });
            });
        });

        table.on('tool(vehicle-refuel-table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除该记录吗？', function (index) {
                    $.ajax({
                        url: 'vehicle-refuels/' + data.id,
                        type: 'DELETE',
                        success: function (result) {
                            if (result.code == 200) {
                                layer.msg('删除成功');
                                table.reload('vehicle-refuel-table');
                            } else {
                                layer.msg('删除失败');
                            }
                        }
                    });
                    layer.close(index);
                });
            } else if (obj.event === 'edit') {
                showVehicleRefuelForm();
                form.val('vehicle-refuel-form', data);
                if (data.state == 1) {
                    $('#audit-btn').show();
                } else {
                    $('#audit-btn').hide();
                }
            }
        });

        form.on('submit(form-audit)', function (data) {
            $.ajax({
                url: '/vehicle-refuels/audit',
                type: 'POST',
                data: {
                    vehicleRefuelId: data.field.id,
                    auditResult: data.field.auditResult,
                    auditComment: data.field.auditComment,
                },
                success: function (result) {
                    if (result.code == 200) {
                        layer.msg('提交成功');
                        table.reload('vehicle-refuel-table');
                    } else {
                        layer.msg('提交失败');
                        console.error('提交失败');
                    }
                }
            });
            closePopupForm();
            return false;
        });

    });
</script>
</body>

</html>